<template>
  <div class="login_box">
    <div class="logo">登录</div>
    <van-form @submit="onSubmit" style="margin-top: 30%">
      <van-cell-group inset>
        <van-field
          v-model="form_data.account"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="form_data.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import http from "@/api/http.js";
import { showNotify } from "vant";
import { useRouter } from "vue-router";
const router = useRouter();

let form_data = ref({
  account: "admin",
  password: "123321",
});

const onSubmit = async (values) => {
  let res = await http.post("admin_login", form_data.value);
  if (res.data.code === "0") {
    localStorage.setItem("token",res.data.token)
    // 成功通知
    showNotify({ type: "success", message: "登录成功" });
    setTimeout(() => {
      router.replace({
        name: "layout",
      });
    }, 500);
  } else {
    showNotify({ type: "danger", message: res.data.msg });
  }
};
</script>

<style scoped>
.login_box {
  height: 100%;
  background-image: url(@/assets/login_bk.jpg);
  background-size: cover;
}
.logo {
  text-align: center;
  color: #fff;
  font-size: 56px;
  font-family: 黑体;
  padding-top: 30%;
}
</style>